package org.rqb.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Slf4j

public class CodeUtil {
    private static final Base64.Encoder encoder = Base64.getEncoder();

    private static final Base64.Decoder decoder = Base64.getDecoder();


    /**
     * Base64 编码
     */
    public static String base64Encode(String input) {
        return Base64.getEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 解码base64
     * @param input
     * @return
     */
    public static String base64Decode(String input) {

        return new String(Base64.getDecoder().decode(input), StandardCharsets.UTF_8);
    }

    /**
     * URL 编码
     */
    public static String urlEncode(String input) {
        try {
            return URLEncoder.encode(input, String.valueOf(StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            log.warn("URL 编码异常", e);
        }
        return null;
    }
    /**
     * URL 编码
     */
    public static String urlDecode(String input) {
        try {
            return URLDecoder.decode(input, String.valueOf(StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            log.warn("URL 解码异常", e);
        }
        return null;
    }


}
